<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <style>
		*{
			margin: 0;
			padding: 0;
		}
        #box1{
            height: 100px;
            width: 100px;
            background-color:red;
            position: absolute;
        }
		
		#boxx{
			width: 140px;
			height: 140px;
			position: absolute;
			top: 60px;
			left: 144px;
			background-color: aqua;
		}
    </style>
    <script>
        window.onload = function(){

            var box1 = document.getElementById("box1");
            var speed = 10;
            var div = 0;//方向
            setInterval(function(){
                switch(dir){
                case 37:
                    // alert("向左");
					console.log(box1.offsetLeft)
					if(box1.offsetLeft<0){
						break
					}else{
						box1.style.left = box1.offsetLeft - speed + "px";
						break;
					}
                   
                case 39:
				console.log(box1.offsetLeft+100)
                    // alert("向右");
                    box1.style.left = box1.offsetLeft + speed + "px";
                    break;
                case 38:
				if(box1.offsetTop<0){
					break
				}else{
					box1.style.top = box1.offsetTop - speed + "px";
					break;
				}
                case 40:
                    // alert("向下");
                    box1.style.top = box1.offsetTop + speed + "px";
                    break;
            }
            },30);

            document.onkeydown = function(event){  
            event = event || window.event;
            // 按住ctrl键加速
            if(event.ctrlKey){
                speed = 50;
            }else{
                speed =10;
            }
            dir = event.keyCode;
            };

             document.onkeyup = function(){
                 dir = 0;
 
            };
        };

    </script>
</head>
<body>
    <div id="box1">
    </div>
	<div id="boxx"></div>
</body>
</html>
